//
//  MSAnchorWordVC.m
//  MSMain
//
//  Created by Dgh Hv on 2018/9/20.
//  Copyright © 2018年 土豆. All rights reserved.
//

#import "MSNewAnchorWordVC.h"
#import "MSInviteVC.h"
#import "MSInviteViewModel.h"
#import "MSAnchorWorkModel.h"
#import "XLIncomeRecordBaseVC.h"
#import "XLTaskViewController.h"
#import "XLAnchorEvaluateVC.h"
#import "BMHCashOutAuthorizeViewController.h"

@interface MSNewAnchorWordVC ()
@property (weak, nonatomic) IBOutlet UILabel *onlineTime;
@property (weak, nonatomic) IBOutlet UIView *onlineInfoView;
@property (weak, nonatomic) IBOutlet UIScrollView *backScrollView;
@property (weak, nonatomic) IBOutlet UILabel *videoCardNum;

@property (weak, nonatomic) IBOutlet UILabel *videoPeopleNum;
@property (weak, nonatomic) IBOutlet UILabel *videoTime;
@property (weak, nonatomic) IBOutlet UILabel *voicePeopleNum;
@property (weak, nonatomic) IBOutlet UILabel *voiceTime;
@property (weak, nonatomic) IBOutlet UILabel *VideoPrice;
@property (weak, nonatomic) IBOutlet UILabel *certificationStatus;

- (IBAction)didClickIncomeDetail:(id)sender;
- (IBAction)didClickRewards:(id)sender;
- (IBAction)didClickEvaluation:(id)sender;
- (IBAction)didClickMakeMoney:(id)sender;
- (IBAction)didClickCertification:(id)sender;

@property (nonatomic,strong) MSInviteViewModel * viewModel;
@property (nonatomic, strong) MSAnchorWorkModel * model;

@end

@implementation MSNewAnchorWordVC

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.backScrollView.contentSize = CGSizeMake(KScreenWidth, 603);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    [self setUI];
    // Do any additional setup after loading the view from its nib.
}

-(void)setUI
{
    self.title = @"工作台";
    ViewRadius(self.onlineInfoView, 4);
    ViewRadius(self.onlineTime, 16);
    [self addNavigationItemWithTitles:@[@"更新"] isLeft:NO target:self action:@selector(didClickRightButton:) tags:nil btnColors:RGB_HEX(0xFFBB01)];
}

#pragma mark ---点击导航栏右按钮  
-(void)didClickRightButton:(UIButton *)btn
{
    btn.userInteractionEnabled = NO;
    [MBProgressHUD showContent:@"正在更新今日数据和登录状态 请勿退出"];
//    [HelpTool startTipViewWithText:@"数据更新中！" duration:2.0f type:kTipViewTypeNone];
    [[TIMManager sharedInstance] logout:^{
        NSString *sign = [XLAccountManager shareInstance].model.Sign;
        TIMLoginParam * login_param = [[TIMLoginParam alloc ]init]; 
        // accountType 和 sdkAppId 通讯云管理平台分配
        // identifier为用户名，userSig 为用户登录凭证
        // appidAt3rd 在私有帐号情况下，填写与sdkAppId 一样
        NSString *TXAPPID = TIM_APP_ID_TEST;
        NSString *TXACCOUNTTYPE = TIM_ACCOUNT_TYPE_TEST;
        NSString *xlapi = BaseURL;
        if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"isTestUrl"]isEqualToString:@"yes"]) {
            xlapi = @"http://test.xinliao.xiuquapp.com";
        }
        if ([xlapi isEqualToString:@"http://xinliao.xiuquapp.com"]) {
            TXAPPID = TIM_APP_ID;   TXACCOUNTTYPE = TIM_ACCOUNT_TYPE;
        }
        login_param.accountType = TXACCOUNTTYPE;
        login_param.identifier = [NSString stringWithFormat:@"%@",[XLAccountManager shareInstance].model.Id];
        login_param.userSig = sign;
        login_param.appidAt3rd = TXAPPID;
        login_param.sdkAppId = [TXAPPID intValue];
        
        [[TIMManager sharedInstance] login:login_param succ:^(){
            
            TIMAPNSConfig * apnsConfig = [[TIMAPNSConfig alloc] init];
            apnsConfig.openPush = 1;
            apnsConfig.c2cSound = @"";
            
            [[TIMManager sharedInstance] setAPNS:apnsConfig succ:^() {
                DebugLog(@"nnnnnnnnnnnnnn");
            } fail:^(int code, NSString *err) {
                DebugLog(@"setAPNS fail %d %@",code , err);
            }];
            
            //视频聊天ILiveSDK登录
            [[XLAccountManager shareInstance] updateUserInfo:[XLAccountManager shareInstance].model];
            [[ILiveLoginManager getInstance] iLiveLogin:[NSString stringWithFormat:@"%@",[XLAccountManager shareInstance].model.Id] sig:sign succ:^{
                if ([[XLAccountManager shareInstance].model.IsAnchor isEqualToString:@"1"]) {
                    [self joinGroup];
                }
                btn.userInteractionEnabled = YES;
                [MBProgressHUD showContent:@"更新成功"];
//                [HelpTool startTipViewWithText:@"更新成功" duration:2.0f type:kTipViewTypeNone];
            } failed:^(NSString *moudle, int errId, NSString *errMsg) {
                btn.userInteractionEnabled = YES;
            }];
        } fail:^(int code, NSString * err) {
            btn.userInteractionEnabled = YES;
        }];
        
    } fail:^(int code, NSString *msg) {
        btn.userInteractionEnabled = YES;
    }];
}

- (void)joinGroup {
    [[TIMGroupManager sharedInstance] JoinGroup:@"10086" msg:@"" succ:^{
        [[TIMGroupManager sharedInstance] ModifyGroupMemberInfoSetNameCard:@"10086" user:[[XLAccountManager shareInstance] model].Id nameCard:@"1" succ:^{
            
        } fail:^(int code, NSString *msg) {
        }];
    } fail:^(int code, NSString *msg) {
        if (code == 10013) {
            [[TIMGroupManager sharedInstance] ModifyGroupMemberInfoSetNameCard:@"10086" user:[[XLAccountManager shareInstance] model].Id nameCard:@"1" succ:^{
            } fail:^(int code, NSString *msg) {
            }];
        } else {
            [MBProgressHUD showContent:@"加群失败" toView:self.view];
        }
    }];
}

-(void)getData
{
    kWeakSelf(self);
    [self.viewModel getAnchorWordComplete:^(MSAnchorWorkModel *model) {
        weakself.model = model;
        [weakself getInfoFromModel];
    }];
}

-(void)getInfoFromModel
{
    {
        NSString * Hour = NSStringFormat(@"%lu",[self.model.OnLineLength integerValue] / 60);
        NSString * Min = NSStringFormat(@"%lu",[self.model.OnLineLength integerValue] % 60);
        if([Hour integerValue] > 0)
        {
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:NSStringFormat(@"%@小时%@分",Hour,Min)];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(24) range:NSMakeRange(0,Hour.length)];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(18) range:NSMakeRange(Hour.length,2)];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(24) range:NSMakeRange(Hour.length + 2,Min.length)];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(18) range:NSMakeRange(str.length-1,1)];
            self.onlineTime.attributedText = str;
        }else
        {
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@分",self.model.OnLineLength]];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(24) range:NSMakeRange(0,str.length-1)];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(18) range:NSMakeRange(str.length-1,1)];
            self.onlineTime.attributedText = str;
        }
    }
    self.videoPeopleNum.text = self.model.VideoChatCount;
    {
        NSString * Hour = NSStringFormat(@"%lu",[self.model.VideoChatLength integerValue] / 60);
        NSString * Min = NSStringFormat(@"%lu",[self.model.VideoChatLength integerValue] % 60);
        if([Hour integerValue] > 0)
        {
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:NSStringFormat(@"%@小时%@分",Hour,Min)];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(24) range:NSMakeRange(0,Hour.length)];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(18) range:NSMakeRange(Hour.length,2)];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(24) range:NSMakeRange(Hour.length + 2,Min.length)];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(18) range:NSMakeRange(str.length-1,1)];
            self.videoTime.attributedText = str;
        }else
        {
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@分",self.model.VideoChatLength]];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(24) range:NSMakeRange(0,str.length-1)];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(18) range:NSMakeRange(str.length-1,1)];
            self.videoTime.attributedText = str;
        }
    }

    self.voicePeopleNum.text = self.model.SoundChatCount;
    {
        NSString * Hour = NSStringFormat(@"%lu",[self.model.SoundChatLength integerValue] / 60);
        NSString * Min = NSStringFormat(@"%lu",[self.model.SoundChatLength integerValue] % 60);
        if([Hour integerValue] > 0)
        {
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:NSStringFormat(@"%@小时%@分",Hour,Min)];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(24) range:NSMakeRange(0,Hour.length)];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(18) range:NSMakeRange(Hour.length,2)];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(24) range:NSMakeRange(Hour.length + 2,Min.length)];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(18) range:NSMakeRange(str.length-1,1)];
            self.voiceTime.attributedText = str;
        }else
        {
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@分",self.model.SoundChatLength]];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(24) range:NSMakeRange(0,str.length-1)];
            [str addAttribute:NSFontAttributeName value:SYSTEMFONT(18) range:NSMakeRange(str.length-1,1)];
            self.voiceTime.attributedText = str;
        }
    }
    
    self.VideoPrice.text = NSStringFormat(@"%@0金豆/分钟",self.model.LiveChatPrice);
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:XiangLove_IsSH] boolValue]) {//
        self.VideoPrice.hidden = YES;
    }
    self.videoCardNum.text = self.model.VideoCards;
    
    switch ([self.model.ContractStatus integerValue]) {
        case 1://审核中
            self.certificationStatus.text = @"审核中";
            break;
        case 2://已经认证
            self.certificationStatus.text = @"已认证";
            break;
        case 3://未认证
            self.certificationStatus.text = @"未认证";
            break;
            
        default:
            break;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - click event

#pragma mark - 收入详情
- (IBAction)didClickIncomeDetail:(id)sender {
    XLIncomeRecordBaseVC *vc = [[XLIncomeRecordBaseVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 任务奖励
- (IBAction)didClickRewards:(id)sender {
    XLTaskViewController *vc = [[XLTaskViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 服务评价
- (IBAction)didClickEvaluation:(id)sender {
    XLAnchorEvaluateVC *vc = [[XLAnchorEvaluateVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 推广赚钱
- (IBAction)didClickMakeMoney:(id)sender {
    MSInviteVC * vc = [[MSInviteVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 实名认证
- (IBAction)didClickCertification:(id)sender {
    
    if ([self.model.ContractStatus integerValue] == 3) {
        BMHCashOutAuthorizeViewController *vc = [[BMHCashOutAuthorizeViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - getters & setters
-(MSInviteViewModel *)viewModel
{
    if(_viewModel == nil)
    {
        _viewModel = [[MSInviteViewModel alloc]init];
    }
    return _viewModel;
}


@end
